<template>
  <div class="list" ref="wrapper">
    <div class="list-in">
        <div class="area">
          <div class="title border-topbottom">当前城市</div>
          <div class="button-list">
            <div class="button-wraper">
              <div class="button">{{this.city}}</div>
            </div>
          </div>
        </div>
        <div class="area">
          <div class="title border-topbottom">热门城市</div>
          <div class="button-list">
            <div class="button-wraper" v-for="item of hot" :key="item.id"
            @click="handleCityClick(item.name)"
            >
              <div class="button">{{item.name}}</div>
            </div>
          </div>
        </div>
        <div class="area" v-for="(item,key) of cities" :key="key"
        :ref="key"
        >
          <div class="title border-topbottom" >{{key}}</div>
          <div class="item-list" v-for="v of item" :key="v.id"
          @click="handleCityClick(v.name)"
          >
            <div class="item border-bottom">{{v.name}}</div>
          </div>
        </div>
    </div>
  </div>
</template>
<script>
import Bscroll from 'better-scroll'
import {mapState, mapMutations} from 'vuex'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    handleCityClick (city) {
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      this.scroll.scrollToElement(this.$refs[this.letter][0])
      // console.log(this.$refs[this.letter])
    }
  }
}
</script>
<style lang="scss" scoped>
@import '~styles/variables' ;
.border-topbottom{
  &:after{
    border-color: #ccc;
  }
  &:before{
    border-color: #ccc;
  }
}
.border-bottom{
  &:before{
    border-color: #ccc;
  }
}
.list{
  overflow: hidden;
  position: absolute;
  top: 1.58rem;
  left: 0;
  right: 0;
  bottom: 0;
  .area{
    .title{
      line-height: .64rem;
      color: #666;
      padding-left: .2rem;
      background: #eee;
    }
    .button-list{
      padding: .1rem .6rem .1rem .1rem;
      overflow: hidden;
      .button-wraper{
        width: 33%;
        float: left;
        .button{
          margin: .1rem;
          border: 1px solid #ccc;
          text-align: center;
          padding: .1rem 0;
          border-radius: .06rem;
        }
      }
    }
    .item-list{
      .item{
        padding-left: .2rem;
        line-height: .72rem;
      }
    }
  }
}
</style>
